#!/bin/bash
# 启动 SkyWalking 数据采集系统 (Docker Compose)

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_DIR"

echo "============================================"
echo "启动 SkyWalking 数据采集系统 (Docker)"
echo "============================================"
echo ""
echo "项目目录: $PROJECT_DIR"
echo ""

# 检查 Docker
if ! command -v docker &> /dev/null; then
    echo "❌ 错误: 找不到 docker 命令"
    echo "请先安装 Docker: https://docs.docker.com/get-docker/"
    exit 1
fi

if ! docker compose version &> /dev/null; then
    echo "❌ 错误: 找不到 docker compose 命令"
    echo "请升级 Docker 到最新版本"
    exit 1
fi

# 检查配置文件
if [ ! -f "config.env" ]; then
    echo "⚠️  警告: config.env 不存在"
    echo "正在从模板复制..."
    cp env.example config.env
    echo ""
    echo "请编辑 config.env 并重新运行此脚本"
    exit 1
fi

# 检查数据目录
mkdir -p data logs

# 显示配置信息
echo "📋 配置信息:"
echo "  - SkyWalking: $(grep SKYWALKING_URL config.env | cut -d'=' -f2)"
echo "  - 数据库: SQLite (data/skywalking_data.db)"
echo "  - Dashboard 端口: 8501"
echo ""

# 启动服务
echo "🚀 启动 Docker Compose 服务..."
docker compose -f docker/docker-compose.yml up -d --build

# 等待服务启动
echo ""
echo "⏳ 等待服务启动..."
sleep 5

# 查看服务状态
docker compose -f docker/docker-compose.yml ps

echo ""
echo "============================================"
echo "✅ 启动完成"
echo "============================================"
echo ""
echo "📊 服务状态"
docker compose -f docker/docker-compose.yml ps

echo ""
echo "📍 访问地址:"
echo "  - Dashboard: http://localhost:8501"
echo ""
echo "🔍 查看日志:"
echo "  docker compose -f docker/docker-compose.yml logs -f collector"
echo "  docker compose -f docker/docker-compose.yml logs -f dashboard"
echo ""
echo "🛑 停止服务:"
echo "  docker compose -f docker/docker-compose.yml down"
echo ""
